package com.ciwong.epaper.modules.evaluate.util;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.evaluate.bean.AnswerRecorder;
import com.ciwong.epaper.modules.evaluate.bean.WordDetail;
import com.ciwong.epaper.modules.evaluate.ui.RereadWordResultActivity;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.epaper.util.ESystem;
import com.ciwong.epaper.util.FileUtil;
import com.ciwong.libs.oralevaluate.IEvaluateEngine;
import com.ciwong.libs.oralevaluate.OnEvaluateListener;
import com.ciwong.libs.oralevaluate.bean.EvaluateResult;
import com.ciwong.libs.oralevaluate.yunzhisheng.YZSEngine;
import com.ciwong.libs.utils.StringUtils;
import com.ciwong.mobilelib.bean.UserInfoBase;
import com.ciwong.mobilelib.ui.BaseActivity;
import com.ciwong.mobilelib.utils.ToastUtil;
import com.unisound.edu.oraleval.sdk.sep15.IOralEvalSDK;
import com.unisound.edu.oraleval.sdk.sep15.SDKError;

import java.io.File;

/**
 * @description:重新评分工具类
 * @author: wyk
 * @time: 2021/7/26 17:29
 */
public class WordResultScoreController implements OnEvaluateListener {

    protected IEvaluateEngine eEngine;

    protected BaseActivity mContext;

    protected String mUuid;

    protected AnswerRecorder answerRecorder;

    protected boolean isSubmitAll;

    private WordResultInterFace wordResultInterFace;

    /**
     * 年级id
     */
    private int mGradeId;
    /**
     * 是否是最后一个需要评分的单词
     */
    private boolean mIsLastOne;

    public void setWordResultInterFace(WordResultInterFace wordResultInterFace) {
        this.wordResultInterFace = wordResultInterFace;
    }

    public WordResultScoreController(BaseActivity mContext, String uuid) {
        this.mContext = mContext;
        this.mUuid = uuid;
        try {
            initYZSEngine();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 初始化
     */
    protected void initYZSEngine() {
        if (EApplication.getInstance().getClazz() != null) {
            mGradeId = EApplication.getInstance().getClazz().getGradeId();
        }
        eEngine = new YZSEngine(mContext);
        if (mGradeId >= 1101 && mGradeId <= 1106) {
            eEngine.SetScoreAdjuest(1.8f);
        } else {
            eEngine.SetScoreAdjuest(1.7f);
        }
        eEngine.setEvaluateListener(WordResultScoreController.this);


    }

    /**
     * 重新评分
     */
    public void startScore(AnswerRecorder answerRecorder, WordDetail word, boolean isLastOne, boolean isSubmitAll) {
        try {
            this.answerRecorder = answerRecorder;
            this.mIsLastOne = isLastOne;
            this.isSubmitAll = isSubmitAll;
            String wordText;
            String soundUrl;
            if (!"".equals(word.getSyllable().trim()) && !"-".equals(word.getSyllable().trim())) {
                eEngine.setText(word.getSyllable());
                wordText = word.getSyllable();
            } else {
                eEngine.setText(word.getWords());
                wordText = word.getWords();
            }
            eEngine.setConnectTimeout(30000);
            //音频opus文件地址
            soundUrl = getSoundOpusPath(word).getAbsolutePath();
            UserInfoBase userInfoBase = EApplication.getInstance().getUserInfoBase();
            if (null != userInfoBase) {
                eEngine.setUid(String.valueOf(userInfoBase.getUserId()));
            }
            eEngine.retry("E", wordText, soundUrl, true);
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (OutOfMemoryError error) {
            error.printStackTrace();
        }

    }

    /**
     * 获取音频地址
     *
     * @param word
     * @return
     */
    private File getSoundPath(WordDetail word) {
        return new File(ESystem.getAnswersUserIdMediaPathWord(mUuid),
                mUuid + StringUtils.md5(word.getWords())
                        + EConstants.AUDIO_DEFAULT_SUFFIX);
    }
    /**
     * 获取音频opus地址
     *
     * @param word
     * @return
     */
    private File getSoundOpusPath(WordDetail word) {
        return new File(ESystem.getAnswersUserIdMediaOpusPathWord(mUuid),
                mUuid + StringUtils.md5(word.getWords()));
    }
    @Override
    public void onInit(int i) {

    }

    @Override
    public void onError(IOralEvalSDK iOralEvalSDK, SDKError sdkError, IOralEvalSDK.OfflineSDKError offlineSDKError) {
        if (mContext instanceof RereadWordResultActivity) {
            ((RereadWordResultActivity) mContext).addScoreCount();
        }
        if (null != wordResultInterFace) {
            wordResultInterFace.notifydata();
        }
        SDKError.Category category = sdkError.category;

    }

    @Override
    public void onStart() {

    }

    @Override
    public void onVolume(int i) {

    }

    @Override
    public void onStop(int i, EvaluateResult evaluateResult, String url) {
        if (mContext instanceof RereadWordResultActivity) {
            ((RereadWordResultActivity) mContext).addScoreCount();
            if (null != wordResultInterFace) {
                wordResultInterFace.setRestartScoreResult(answerRecorder, evaluateResult,url, mIsLastOne, isSubmitAll);
            }
            if (!mIsLastOne) {
                ((RereadWordResultActivity) mContext).scoreRetry();
            }
        }
    }

    @Override
    public void onStartOralEval() {

    }

    public interface WordResultInterFace {
        void setRestartScoreResult(AnswerRecorder answerRecorder, EvaluateResult evaluateResult,String yzsAudioUrl, boolean mIsLastOne, boolean isSubmitAll);

        void notifydata();

        void scoreError();
    }


}
